/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package course.ya.doc.from.code.model.statements;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Transient;

/**
 *
 * @author Denis
 */
@Entity
@DiscriminatorValue("MethodDeclarationStatement")
public class MethodDeclarationStatement extends AStatement {
    @Column(name = "method_declaration_expression")
    private String condition;

    @Transient
    private Pattern pattern;
    @Transient
    private Matcher matcher;
    @Transient
    private static final String STRING_PATTERN = "^(public|private|protected)?\\s*([_A-z]\\w*)"
            + "(<.+>)?  \\s+([_A-z]\\w*\\s*)[(]((?s).*)[)]"
                + "(\\s+(throws)?\\s+(\\w+\\s*,?\\s*\\w*)*)*$";

    public MethodDeclarationStatement() {
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }

    public MethodDeclarationStatement(String str) {
        this.pattern = Pattern.compile(STRING_PATTERN, Pattern.COMMENTS | Pattern.DOTALL);
        this.condition = findStr(str);
    }

    public String findStr(String input) {
        matcher = pattern.matcher(input);
        if (matcher.find()) {
            return matcher.group();
        }
        else {
            return null;
        }
    }

    @Override
    public String toString() {
        return condition;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 73 * hash + (this.condition != null ? this.condition.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final MethodDeclarationStatement other = (MethodDeclarationStatement) obj;
        if ((this.condition == null) ? (other.condition != null) : !this.condition.equals(other.condition)) {
            return false;
        }
        return true;
    }

}
